﻿using Electric.Core;
using Electric.Domain.Entitys.Commons;
using Electric.Domain.Shared.Entitys.Identity;
using Microsoft.AspNetCore.Identity;
using System.Security.Claims;

namespace Electric.Domain.Entitys.Status;


public class IpInfo : AuditedAggregateRoot<Guid>
{
    /// <summary>
    /// IpAddress
    /// </summary>
    public string IpAddress { get; protected set; }

    /// <summary>
    /// Name
    /// </summary>
    public string? Name { get; protected set; }

    /// <summary>
    /// Location
    /// </summary>
    public string? Location { get; protected set; }


    /// <summary>
    /// Cpe
    /// </summary>
    public string? Cpe { get; protected set; }


    /// <summary>
    /// Domain 
    /// </summary>
    public string? Domain { get; protected set; }

    /// <summary>
    /// mac 
    /// </summary>
    public string? Mac { get; protected set; }

    /// <summary>
    /// 是否激活 
    /// </summary>
    public bool Enable { get; protected set; }


    /// <summary>
    /// 标准化mac
    /// </summary>
    public string? NormalizedMac { get; protected internal set; }


    /// <summary>
    /// 备注
    /// </summary>
    public string? Remark { get; set; }


    /// <summary>
    /// 角色列表
    /// </summary>
    public List<InfoStatus> Statuses { get; protected set; }

    protected IpInfo()
    {
    }

    public IpInfo(Guid id, string ipAddress, string? name = null, string? location = null, string? cpe = null, string? domain = null, string? mac = null, bool enable = true, string? remark = null) : base(id)
    {
        Check.NotNull(ipAddress, nameof(ipAddress));
        IpAddress = ipAddress;
        Name = name;
        Location = location;
        Cpe = cpe;
        Domain = domain;
        Mac = mac;
        NormalizedMac = mac?.ToUpperInvariant();
        Enable = enable;
        Remark = remark;
        Statuses = new List<InfoStatus>();
    }

    public void AddStatuses(Guid ipStatusId)
    {
        Check.NotNull(ipStatusId, nameof(ipStatusId));
        Statuses.Add(new InfoStatus(Id, ipStatusId));
    }

    public void RemoveStatuses(Guid ipStatusId)
    {
        Check.NotNull(ipStatusId, nameof(ipStatusId));
        if (!IsInStatuses(ipStatusId))
        {
            return;
        }
        Statuses.RemoveAll(r => r.IpInfoId == ipStatusId);
    }

    public bool IsInStatuses(Guid ipStatusId)
    {
        Check.NotNull(ipStatusId, nameof(ipStatusId));
        return Statuses.Any(r => r.IpInfoId == ipStatusId);
    }


    public void RemoveAllStatuses()
    {
        Statuses = new List<InfoStatus>();
    }

}

